using System;
using System.Threading;

using SKYPE4COMLib;


namespace SkypeNet
{
    public class Skype1
    {

        private SkypeClass skypeClass = null;
        //private EventWaitHandle sema = null;
        private Exception lastError = null;

        public Skype1()
        {
            //sema = new EventWaitHandle(false, EventResetMode.AutoReset);

        }

        ~Skype1()
        {
            /*if (sema != null)
            {
                sema.Close();
                sema = null;
            }*/
        }
        /// <summary>
        /// 
        /// </summary>
        public void SendSMS(string recipient, string text)
        {
            if (skypeClass == null)
            {
                skypeClass = new SkypeClass();
                skypeClass.SmsMessageStatusChanged += new _ISkypeEvents_SmsMessageStatusChangedEventHandler(skypeClass_SmsMessageStatusChanged);
            }            
            
            if (!skypeClass.Client.IsRunning)
            {
                skypeClass.Client.Start(true, true);
                // wait for load
                while (!skypeClass.Client.IsRunning)
                    System.Threading.Thread.Sleep(2000);
                skypeClass.Attach(5, false);
                // wait for attach
                System.Threading.Thread.Sleep(2000);

                while (skypeClass.AttachmentStatus != TAttachmentStatus.apiAttachSuccess)
                {
                    switch (skypeClass.AttachmentStatus)
                    {
                        case TAttachmentStatus.apiAttachAvailable:
                        case TAttachmentStatus.apiAttachUnknown:
                            skypeClass.Attach(5, false);
                            break;
                        case TAttachmentStatus.apiAttachRefused:
                            throw new Exception("Skype Attachement Refused!");
                            break;
                        case TAttachmentStatus.apiAttachNotAvailable:
                            throw new Exception("Skype Attachement not Available!");
                            break;
                    }                      

                    System.Threading.Thread.Sleep(1000);

                }
            }

            lastError = null;
            /*SmsMessage msg = skypeClass.SendSms(recipient, text, skypeClass.CurrentUser.PhoneMobile);*/
            SmsMessage msg = skypeClass.CreateSms(TSmsMessageType.smsMessageTypeOutgoing, recipient);
            msg.Body = text;
            msg.Send();

            while (msg.Status != TSmsMessageStatus.smsMessageStatusReceived && msg.Status != TSmsMessageStatus.smsMessageStatusFailed)
            {
                Thread.Sleep(1000);
                if (lastError != null)
                    break;
            }

            msg = null;

            /*if (!sema.WaitOne(120 * 1000, false))
            {
                throw new Exception("Skype Send Sms Timeout Error");
            }*/

            if (lastError != null)
                throw lastError; 
            
        }

        void skypeClass_SmsMessageStatusChanged(SmsMessage pMessage, TSmsMessageStatus Status)
        {
            switch (Status)
            {
                case TSmsMessageStatus.smsMessageStatusFailed:
                    lastError = new Exception("Skype Send Failed: " + pMessage.FailureReason);
                    //sema.Set();
                    break;
                case TSmsMessageStatus.smsMessageStatusReceived:
                    //sema.Set();
                    break;
            }
        }
    }
}
